﻿//天地图

//矢量
define([
    "dojo/_base/declare"
    , "esri/layers/TiledMapServiceLayer"
    , "esri/SpatialReference"
    , "esri/geometry/Extent"
    , "esri/layers/TileInfo"
    , "esri/request"
], function (declare
    , TiledMapServiceLayer
    , SpatialReference
    , Extent
    , TileInfo
    , esriRequest) {
    var Sip_vec_c = declare([TiledMapServiceLayer], {
        constructor: function (options) {
            this.preUrl=options.preUrl;
            this.token=options.token;
            this.wkt = "PROJCS[\"Suzhou_1954_3_Degree_GK_CM_120E\",GEOGCS[\"GCS_Beijing_1954\",DATUM[\"D_Beijing_1954\",SPHEROID[\"Krasovsky_1940\",6378245.0,298.3]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Gauss_Kruger\"],PARAMETER[\"False_Easting\",50805.0],PARAMETER[\"False_Northing\",-3421129.0],PARAMETER[\"Central_Meridian\",120.5833333333333],PARAMETER[\"Scale_Factor\",1.0],PARAMETER[\"Latitude_Of_Origin\",0.0],UNIT[\"Meter\",1.0]]";
            this.spatialReference = new SpatialReference({wkt: this.wkt});
            this.initialExtent = (this.fullExtent = new Extent(49958.666735942475, 34997.858768371865, 80015.39351606369,
                60383.91201311909, this.spatialReference));
            this.tileInfo = new TileInfo({
                "rows": 256,
                "cols": 256,
                "compressionQuality": 0,
                "origin": {
                    "x": 50000.0,
                    "y": 60000.0
                },
                "spatialReference": {
                    "wkt": this.wkt
                },
                "lods": [{"level": 0, "scale": 500000.0, "resolution": 132.2919312505292},
                    {"level": 1, "scale": 200000.0, "resolution": 52.91677250021167},
                    {"level": 2, "scale": 100000.0, "resolution": 26.458386250105836},
                    {"level": 3, "scale": 50000.0, "resolution": 13.229193125052918},
                    {"level": 4, "scale": 20000.0, "resolution": 5.291677250021167},
                    {"level": 5, "scale": 10000.0, "resolution": 2.6458386250105836},
                    {"level": 6, "scale": 5000.0, "resolution": 1.3229193125052918},
                    {"level": 7, "scale": 2000.0, "resolution": 0.5291677250021167},
                    {"level": 8, "scale": 1000.0, "resolution": 0.26458386250105836}]
            });
            this.loaded = true;
            this.onLoad(this);
        },
        getTileUrl: function (level, row, col) {
            var me = this;
            var url = `${me.preUrl}/SIPGIS/SIPSD_sip/2017img/tile/${level}/${row}/${col}?sipsdToken=${me.token}`;
            if (me.token) {
                return url;
            } else {
                me.getToken(function (token) {
                    me.token = token;
                    return url;
                });
            }
            ;
        },
        getToken: function (callback) {
            var me = this;

            $.ajax({
                type: "post",
                async: false,
                contentType: "application/json;charset=utf-8",
                url: `${me.preUrl}/CIC/SIPGIS/auth/jwt/token`,
                data: JSON.stringify({
                    "username": "bandaohuanbao",
                    "password": "123456",
                    "appId": "BASE-ADMIN"
                }),
                dataType: 'json',
                success: function (result) {
                    var obj = eval(result);

                    if (obj.code == 200) {
                        var token = encodeURI(obj.token);
                        if ($.isFunction(callback)) {
                            callback(token);
                        }
                    }
                    ;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest);
                    console.log(textStatus);
                    console.log(errorThrown);
                }
            });
        }
    });
    return Sip_vec_c;
});

